#include "thread.h"
#include <stdio.h>
#include <stdlib.h>
  #include <time.h>

int compteur=1;

void* fibonacci_test(void* arg){

	int n= *(int*)arg;
 
        int* result = malloc(sizeof(int));
  
        *result = n;
	
 
	if(n<2){
              
               *result = 1;
	       thread_exit(result);
		
	}
        
	int* p =malloc(sizeof(int));
       
        *p=n-1;

	compteur++;

	int* q=malloc(sizeof(int));
        
        *q=n-2;

	thread_t thread1,thread2;
	thread_create(&thread1,fibonacci_test,p);
	thread_create(&thread2,fibonacci_test,q);
       

	int* retval1=(int*)malloc(sizeof(int));
	int* retval2=(int*)malloc(sizeof(int));

	thread_join(thread1,&retval1);
      
	thread_join(thread2,&retval2);
      

	*result=*retval1+*retval2;

	free(retval1);
	free(retval2);
        free(p);
	free(q);

	return result;
}



int main(int argc,void * argv[]){
clock_t start, end;

    start = clock ();
    {
printf("Veuillez choisir un argument pour la fonction fibonacci :\n");
  scanf("%d",&argc);
  printf("Vous avez choisi le nombre : %d\n", argc);
	
  int* result = malloc(sizeof(int));
  thread_t thread0;
  thread_create(&thread0,fibonacci_test,&argc);


  thread_join(thread0,&result);
  printf("Voici le résultat obtenu pour l'argument %d après le lancement de %d threads= %d\n",argc,compteur,*result);
  free(result);
}
  end = clock ();
  printf ("Temps en secondes : %f\n", (end - start) / (double)CLOCKS_PER_SEC);
  
  return 0;
}





